<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="流程名称" prop="processName">
        <el-input
          v-model="queryParams.processName"
          placeholder="请输入流程名称"
          clearable
        />
      </el-form-item>
      <el-form-item label="业务名称" prop="messageTitle">
        <el-input
          v-model="queryParams.messageTitle"
          placeholder="请输入业务名称"
          clearable
        />
      </el-form-item>
      <el-form-item label="流程分类" prop="category">
        <el-select
          v-model="queryParams.category"
          clearable
          placeholder="请选择流程分类"
        >
          <el-option
            v-for="item in categoryOptions"
            :key="item.categoryId"
            :label="item.categoryName"
            :value="item.code"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="提交时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="defaultTime"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        <el-button
          type="primary"
          plain
          icon="el-icon-delete"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
        <!-- <el-button
          type="warning"
          plain
          icon="el-icon-download"
          @click="handleExport"
          >导出</el-button
        > -->
        <el-button
          type="primary"
          plain
          :disabled="single"
          icon="el-icon-circle-close"
          @click="handleStop()"
          >取消</el-button
        >
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      :data="ownProcessList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column
        label="流程名称"
        prop="procDefName"
        :show-overflow-tooltip="true"
      >
        <template #default="scope">
          <el-button type="primary" text @click="handleFlowRecord(scope.row)">{{
            scope.row.procDefName
          }}</el-button>
        </template>
      </el-table-column>
      <el-table-column
        label="业务名称"
        prop="messageTitle"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        label="流程类别"
        prop="category"
        :formatter="categoryFormat"
      />
      <el-table-column label="当前节点" prop="taskName" />
      <el-table-column label="提交时间" prop="createTime" width="180" />
      <el-table-column label="流程状态" width="100">
        <template #default="scope">
          <el-tag v-if="scope.row.finishTime == null">进行中</el-tag>
          <el-tag type="success" v-if="scope.row.finishTime != null"
            >已完成</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column label="耗时" prop="duration" width="180" />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script name="Own" setup>
import { listOwnProcess, stopProcess, delProcess } from '@/api/workflow/process'
import { listAllCategory } from '@/api/workflow/category'
import { useRoute, useRouter } from 'vue-router'
const router = useRouter()
const { proxy } = getCurrentInstance()
const data = reactive({
  // 遮罩层
  loading: true,
  processLoading: true,
  // 选中数组
  ids: [],
  // 非单个禁用
  single: true,
  // 非多个禁用
  multiple: true,
  // 显示搜索条件
  showSearch: true,
  // 总条数
  total: 0,
  categoryOptions: [],
  processTotal: 0,
  // 我发起的流程列表数据
  ownProcessList: [],
  // 弹出层标题
  title: '',
  // 是否显示弹出层
  open: false,
  src: '',
  definitionList: [],
  // 日期范围
  dateRange: [],
  // 查询参数
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    messageTitle: undefined,
    processKey: undefined,
    processName: undefined,
    category: undefined
  },
  // 表单参数
  form: {},
  // 表单校验
  rules: {}
})
const {
  // 遮罩层
  loading,
  processLoading,
  // 选中数组
  ids,
  // 非单个禁用
  single,
  // 非多个禁用
  multiple,
  // 显示搜索条件
  showSearch,
  // 总条数
  total,
  categoryOptions,
  processTotal,
  // 我发起的流程列表数据
  ownProcessList,
  // 弹出层标题
  title,
  // 是否显示弹出层
  open,
  src,
  definitionList,
  // 日期范围
  dateRange,
  // 查询参数
  queryParams,
  // 表单参数
  form,
  // 表单校验
  rules
} = toRefs(data)
const defaultTime = ref([
  new Date(2000, 1, 1, 0, 0, 0),
  new Date(2000, 2, 1, 23, 59, 59)
])

/** 查询流程分类列表 */
function getCategoryList() {
  listAllCategory().then(response => (categoryOptions.value = response.data))
}
/** 查询流程定义列表 */
function getList() {
  loading.value = true
  listOwnProcess(proxy.addDateRange(queryParams.value, dateRange.value)).then(
    response => {
      ownProcessList.value = response.rows
      total.value = response.total
      loading.value = false
    }
  )
}
// 取消按钮
function cancel() {
  open.value = false
  reset()
}
// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    category: null,
    key: null,
    tenantId: null,
    deployTime: null,
    derivedFrom: null,
    derivedFromRoot: null,
    parentDeploymentId: null,
    engineVersion: null
  }
  proxy.resetForm('form')
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}
/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = []
  proxy.resetForm('queryForm')
  handleQuery()
}
// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.procInsId)
  single.value = selection.length !== 1
  multiple.value = !selection.length
}
/**  取消流程申请 */
function handleStop(row) {
  const params = {
    procInsId: row.procInsId || ids.value.toString()
  }
  stopProcess(params).then(res => {
    proxy.$modal.msgSuccess('操作成功')
    getList()
  })
}
/** 流程流转记录 */
function handleFlowRecord(row) {
  console.log(row.procInsId, 'row.procInsId--')
  router.push({
    path: '/process/workDetail',
    query: {
      procInsId: row.procInsId,
      processed: false
    }
  })
}
/** 删除按钮操作 */
function handleDelete(row) {
  console.log(row.procInsId, ids.value, 'row.procInsId---')
  const procInsId = row.procInsId || ids.value
  proxy.$modal
    .confirm('是否确认删除该数据项？')
    .then(() => {
      loading.value = true
      return delProcess(procInsId)
    })
    .then(() => {
      loading.value = false
      getList()
      proxy.$modal.msgSuccess('删除成功')
    })
    .finally(() => {
      loading.value = false
    })
}
/** 导出按钮操作 */
function handleExport() {
  proxy.downLoadByUrl(
    'bpmn/workflow/process/ownExport',
    {
      ...queryParams.value
    },
    `wf_own_process_${new Date().getTime()}.xlsx`
  )
}
function categoryFormat(row, column) {
  return (
    categoryOptions.value.find(k => k.code === row.category)?.categoryName ?? ''
  )
}
getCategoryList()
getList()
</script>
